﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgramShortcuts.Components.Functions;

namespace ProgramShortcuts.Components.Services
{
	public class ConsoleInputServiceImpl : IConsoleInputService
	{	
		IFunctionState[] _Functions;
		int _SelectedFunction;
		public ConsoleInputServiceImpl(IFunctionState[] functionStates)
		{
			this._Functions = functionStates;
			_SelectedFunction = -1;
			//this._Functions.InformUserOfCurrentState();
			
		}
		#region IConsoleInputService Members
		
		public void Handle( ConsoleKeyInfo consoleKey )
		{
			if ( _Functions == null || _Functions.Length == 0 )
			{
				return;
			}
			if ( consoleKey.Key == ConsoleKey.Tab )
			{
				if ( _SelectedFunction + 1 >= _Functions.Length ) _SelectedFunction = -1;
				_SelectedFunction++;
				_Functions[_SelectedFunction].InformUserOfCurrentState();
				return;
			}
			if ( consoleKey.Key == ConsoleKey.Escape )
			{
				return;
			}
			_Functions[_SelectedFunction].InteractWithUser( consoleKey );
			//
			//while ( null != _Functions )
			//{
			//    this._Functions = _Functions.TransitionToNextState( consoleKey );
			//    consoleKey = Console.ReadKey( true );
			//    if ( consoleKey.Key == ConsoleKey.Escape )
			//    {
			//        this._Functions = null;
			//    }
			//}
		}

		//private static void ClearCurrentLine()
		//{
		//    Console.SetCursorPosition( 0, Console.CursorTop );
		//    for ( int character = 0; character < Console.BufferWidth - 1; character++ )
		//    {
		//        Console.Write( ' ' );
		//    }
		//    Console.SetCursorPosition( 0, Console.CursorTop );
		//    return;
		//}

		//private void SpacesToLength( int p )
		//{
		//    for ( int i = 0; i < p; i++ )
		//    {
		//        Console.Write( ' ' );
		//    }
		//}

		#endregion
	}
}
